package com.john.bms.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author:张家小帅哥
 * @Date:2020/11/19 10:30
 * @Version:1.0
 * @Description:
 */
public class ExcelUtil {

    public static void writeExcel(HttpServletResponse response, List<?> list, String fileName, String sheetName, Class clazz)throws Exception  {
        response.setCharacterEncoding("utf8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" +     URLEncoder.encode(fileName + ".xlsx", "UTF-8")); //文件名
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");

        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        ExcelWriter writer = new ExcelWriter(null, response.getOutputStream(), ExcelTypeEnum.XLSX, true, null);
        Sheet sheet1 = new Sheet(1, 1, clazz, "含供应商和地区", null);
        sheet1.setAutoWidth(true);
        writer.write(list, sheet1);
        writer.finish();
//        EasyExcel.write(response.getOutputStream(), clazz)
//                .sheet(sheetName) //sheet名
//                .doWrite(list);
    }
}
